{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Pattern Generator and Trace Analyzer\n",
    "\n",
    "This notebook will show how to use the Pattern Generator to generate patterns on I/O pins. The pattern that will be generated is 3-bit up count performed 4 times. "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Step 1: Download the `logictools` overlay"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true,
    "run_control": {
     "frozen": false,
     "read_only": false
    }
   },
   "outputs": [],
   "source": [
    "from pynq.overlays.logictools import LogicToolsOverlay\n",
    "\n",
    "logictools_olay = LogicToolsOverlay('logictools.bit')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Step 2: Create WaveJSON waveform\n",
    "The pattern to be generated is specified in the waveJSON format   \n",
    "\n",
    "The pattern is applied to the Arduino interface, pins **D0**, **D1** and **D2** are set to generate a 3-bit count. \n",
    "To check the generated pattern we loop them back to pins **D19**, **D18** and **D17** respectively and use the the trace analyzer to view the loopback signals\n",
    "\n",
    "The Waveform class is used to display the specified waveform."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div class=\"output_svg\"><img class=\"svg\" style=\"max-width: none\"src=\"\" alt=\"Image\"></img></div>"
      ],
      "text/plain": [
       "<IPython.core.display.HTML object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "from pynq.lib.logictools import Waveform\n",
    "\n",
    "up_counter = {'signal': [\n",
    "    ['stimulus',\n",
    "        {'name': 'bit0', 'pin': 'D0', 'wave': 'lh' * 8},\n",
    "        {'name': 'bit1', 'pin': 'D1', 'wave': 'l.h.' * 4},\n",
    "        {'name': 'bit2', 'pin': 'D2', 'wave': 'l...h...' * 2}], \n",
    "      \n",
    "    ['analysis',\n",
    "        {'name': 'bit2_loopback', 'pin': 'D17'},\n",
    "        {'name': 'bit1_loopback', 'pin': 'D18'},\n",
    "        {'name': 'bit0_loopback', 'pin': 'D19'}]], \n",
    "\n",
    "    'foot': {'tock': 1},\n",
    "    'head': {'text': 'up_counter'}}\n",
    "\n",
    "waveform = Waveform(up_counter)\n",
    "waveform.display()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**Note:** Since there are no captured samples at this moment, the analysis group will be empty."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Step 3: Instantiate the pattern generator and trace analyzer objects\n",
    "Users can choose whether to use the trace analyzer by calling the `trace()` method.    \n",
    "The analyzer can be set to trace a specific number of samples using, `num_analyzer_samples` argument."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": true,
    "run_control": {
     "frozen": false,
     "read_only": false
    }
   },
   "outputs": [],
   "source": [
    "pattern_generator = logictools_olay.pattern_generator\n",
    "pattern_generator.trace(num_analyzer_samples=16)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Step 4: Setup the pattern generator\n",
    "The pattern generator will work at the default frequency of 10MHz. This can be modified using a `frequency` argument in the `setup()` method. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "pattern_generator.setup(up_counter,\n",
    "                        stimulus_group_name='stimulus',\n",
    "                        analysis_group_name='analysis')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "__Set the loopback connections using jumper wires on the Arduino Interface__\n",
    "![](./images/pattern_generator_wiring.png)\n",
    "\n",
    "* __Output pins D0, D1 and D2 are connected to pins D19, D18 and D17 respectively__ \n",
    "* __Loopback/Input pins D19, D18 and D17 are observed using the trace analyzer as shown below__\n",
    "* __After setup, the pattern generator should be ready to run__\n",
    "\n",
    "**Note:** Make sure all other pins are disconnected."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Step 5: Run and display waveform\n",
    "\n",
    "The ` run()` method will execute all the samples, `show_waveform()` method is used to display the waveforms.   \n",
    "Alternatively, we can also use `step()` method to single step the pattern."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div class=\"output_svg\"><img class=\"svg\" style=\"max-width: none\"src=\"\" alt=\"Image\"></img></div>"
      ],
      "text/plain": [
       "<IPython.core.display.HTML object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "pattern_generator.run()\n",
    "pattern_generator.show_waveform()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Step 6: Stop the pattern generator\n",
    "Calling `stop()` will clear the logic values on output pins; however, the waveform will be recorded locally in the pattern generator instance."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "pattern_generator.stop()"
   ]
  }
 ],
 "metadata": {
  "anaconda-cloud": {},
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.0"
  },
  "nbpresent": {
   "slides": {
    "07899ba6-25e4-4425-8a83-ced90a84d430": {
     "id": "07899ba6-25e4-4425-8a83-ced90a84d430",
     "prev": "58ba9a60-18b5-4988-8fc9-9e5e0a761a44",
     "regions": {
      "afb41902-0dfd-4f57-b9d1-765ff7cd38e2": {
       "attrs": {
        "height": 0.8,
        "width": 0.8,
        "x": 0.1,
        "y": 0.1
       },
       "content": {
        "cell": "c65c7ff7-8bea-4021-b29b-1febf14436dd",
        "part": "whole"
       },
       "id": "afb41902-0dfd-4f57-b9d1-765ff7cd38e2"
      }
     }
    },
    "08366e17-c8fb-4e81-ac4f-acb3ffdbc5db": {
     "id": "08366e17-c8fb-4e81-ac4f-acb3ffdbc5db",
     "prev": "cf589142-353f-4315-ace7-d5aa2c5cd3dd",
     "regions": {
      "6782802f-f10a-432c-8dc0-24515e875d40": {
       "attrs": {
        "height": 0.8,
        "width": 0.8,
        "x": 0.1,
        "y": 0.1
       },
       "content": {
        "cell": "c72b3736-696f-4377-ae44-2ffc5590beea",
        "part": "whole"
       },
       "id": "6782802f-f10a-432c-8dc0-24515e875d40"
      }
     }
    },
    "0c7aa757-342b-467c-8897-da18edc26325": {
     "id": "0c7aa757-342b-467c-8897-da18edc26325",
     "prev": "ac1c491b-978f-46b2-a804-8b226ce12445",
     "regions": {
      "aeea45fa-b289-4b19-b470-46c2412978d5": {
       "attrs": {
        "height": 0.8,
        "width": 0.8,
        "x": 0.1,
        "y": 0.1
       },
       "content": {
        "cell": "8d7dc312-27a9-490d-abfb-eff827a0669d",
        "part": "whole"
       },
       "id": "aeea45fa-b289-4b19-b470-46c2412978d5"
      }
     }
    },
    "1c8fbe49-cf1a-4c13-9149-87396f5d2d43": {
     "id": "1c8fbe49-cf1a-4c13-9149-87396f5d2d43",
     "prev": "c66f3737-7106-42fa-8299-56d309c7f58b",
     "regions": {
      "0838d610-e6b7-4677-84b2-4d75fa82616c": {
       "attrs": {
        "height": 0.8,
        "width": 0.8,
        "x": 0.1,
        "y": 0.1
       },
       "content": {
        "cell": "108dbebc-aa60-4e16-bed7-a49fc963326b",
        "part": "whole"
       },
       "id": "0838d610-e6b7-4677-84b2-4d75fa82616c"
      }
     }
    },
    "2308a269-bda0-4dcb-8f56-08cd8515481c": {
     "id": "2308a269-bda0-4dcb-8f56-08cd8515481c",
     "prev": "24734f0c-f62b-4d80-8bb1-b72807a1db62",
     "regions": {
      "2cbe2352-6041-49a6-8915-3b09d84c7eec": {
       "attrs": {
        "height": 0.8,
        "width": 0.8,
        "x": 0.1,
        "y": 0.1
       },
       "content": {
        "cell": "9aeda803-b81c-4005-8a90-27501e431290",
        "part": "whole"
       },
       "id": "2cbe2352-6041-49a6-8915-3b09d84c7eec"
      }
     }
    },
    "24734f0c-f62b-4d80-8bb1-b72807a1db62": {
     "id": "24734f0c-f62b-4d80-8bb1-b72807a1db62",
     "prev": "6148ba10-d53e-4f7f-b114-9a47a9c04809",
     "regions": {
      "f0b4c308-9774-4c45-96d0-35391057d581": {
       "attrs": {
        "height": 0.8,
        "width": 0.8,
        "x": 0.1,
        "y": 0.1
       },
       "content": {
        "cell": "c87d8ffb-edc1-4f74-a148-85379fe95e95",
        "part": "whole"
       },
       "id": "f0b4c308-9774-4c45-96d0-35391057d581"
      }
     }
    },
    "272b26e0-ad15-4290-a72d-c0ebc37ac87c": {
     "id": "272b26e0-ad15-4290-a72d-c0ebc37ac87c",
     "prev": "2308a269-bda0-4dcb-8f56-08cd8515481c",
     "regions": {
      "b58b2653-d5c5-42a7-bb5f-24175a6cb77f": {
       "attrs": {
        "height": 0.8,
        "width": 0.8,
        "x": 0.1,
        "y": 0.1
       },
       "content": {
        "cell": "b283db1c-e40a-41f1-8c0d-5750db13ba82",
        "part": "whole"
       },
       "id": "b58b2653-d5c5-42a7-bb5f-24175a6cb77f"
      }
     }
    },
    "3feaf1fb-eeb4-433c-9dbf-3c1775a252db": {
     "id": "3feaf1fb-eeb4-433c-9dbf-3c1775a252db",
     "prev": "eebe3b35-f197-4cd1-885f-4536bd79fc1e",
     "regions": {
      "b7c4f415-8d03-4670-9539-fb11144b11ee": {
       "attrs": {
        "height": 0.8,
        "width": 0.8,
        "x": 0.1,
        "y": 0.1
       },
       "content": {
        "cell": "dadb569b-c20d-48e0-9c91-0614867568c3",
        "part": "whole"
       },
       "id": "b7c4f415-8d03-4670-9539-fb11144b11ee"
      }
     }
    },
    "4b01980b-925d-4d71-9da2-bce29afd2368": {
     "id": "4b01980b-925d-4d71-9da2-bce29afd2368",
     "prev": "d6180808-ff71-4606-960f-ea33915d76fc",
     "regions": {
      "6e04c87a-fa70-4958-b959-aaf94f94913c": {
       "attrs": {
        "height": 0.8,
        "width": 0.8,
        "x": 0.1,
        "y": 0.1
       },
       "content": {
        "cell": "333c5227-9c43-4b1b-824b-3e4ddd188651",
        "part": "whole"
       },
       "id": "6e04c87a-fa70-4958-b959-aaf94f94913c"
      }
     }
    },
    "58ba9a60-18b5-4988-8fc9-9e5e0a761a44": {
     "id": "58ba9a60-18b5-4988-8fc9-9e5e0a761a44",
     "prev": "272b26e0-ad15-4290-a72d-c0ebc37ac87c",
     "regions": {
      "d530c786-af70-4107-944d-d1cdbf5ea9da": {
       "attrs": {
        "height": 0.8,
        "width": 0.8,
        "x": 0.1,
        "y": 0.1
       },
       "content": {
        "cell": "0edabdd6-d23b-4a01-a709-5fe95575eb11",
        "part": "whole"
       },
       "id": "d530c786-af70-4107-944d-d1cdbf5ea9da"
      }
     }
    },
    "6148ba10-d53e-4f7f-b114-9a47a9c04809": {
     "id": "6148ba10-d53e-4f7f-b114-9a47a9c04809",
     "prev": "aac4e37d-47e7-4807-b784-80de55d37f98",
     "regions": {
      "578a3f8a-0c1f-4980-b25c-10889ffa974c": {
       "attrs": {
        "height": 0.8,
        "width": 0.8,
        "x": 0.1,
        "y": 0.1
       },
       "content": {
        "cell": "4900f181-bc06-478e-8194-939ef978bd8d",
        "part": "whole"
       },
       "id": "578a3f8a-0c1f-4980-b25c-10889ffa974c"
      }
     }
    },
    "706271b3-dc1d-4004-8bda-8f6c8a9f71cb": {
     "id": "706271b3-dc1d-4004-8bda-8f6c8a9f71cb",
     "prev": "d2dac60f-d9f1-4455-9b2f-23f93393f241",
     "regions": {
      "26c27058-279d-40a1-94c1-3f1c3ca78882": {
       "attrs": {
        "height": 0.8,
        "width": 0.8,
        "x": 0.1,
        "y": 0.1
       },
       "content": {
        "cell": "e83af67a-0650-40ca-b625-ae99a36a77e4",
        "part": "whole"
       },
       "id": "26c27058-279d-40a1-94c1-3f1c3ca78882"
      }
     }
    },
    "a63b435f-5349-46b8-928f-efc8f22a3e4f": {
     "id": "a63b435f-5349-46b8-928f-efc8f22a3e4f",
     "prev": null,
     "regions": {
      "eb482349-2787-49a2-9d04-49a429cb8f7c": {
       "attrs": {
        "height": 0.8,
        "width": 0.8,
        "x": 0.1,
        "y": 0.1
       },
       "content": {
        "cell": "9416c43b-556a-492e-a242-ac249ab5333a",
        "part": "whole"
       },
       "id": "eb482349-2787-49a2-9d04-49a429cb8f7c"
      }
     }
    },
    "a8f25ad5-2754-4843-aff0-773bbe089675": {
     "id": "a8f25ad5-2754-4843-aff0-773bbe089675",
     "prev": "a63b435f-5349-46b8-928f-efc8f22a3e4f",
     "regions": {
      "18c60535-774e-4fca-9bc9-7d7acdfa1219": {
       "attrs": {
        "height": 0.8,
        "width": 0.8,
        "x": 0.1,
        "y": 0.1
       },
       "content": {
        "cell": "5b7c3ddd-5274-401d-b4ce-c7a64c541d7d",
        "part": "whole"
       },
       "id": "18c60535-774e-4fca-9bc9-7d7acdfa1219"
      }
     }
    },
    "aac4e37d-47e7-4807-b784-80de55d37f98": {
     "id": "aac4e37d-47e7-4807-b784-80de55d37f98",
     "prev": "4b01980b-925d-4d71-9da2-bce29afd2368",
     "regions": {
      "7b9f1541-92e5-43dd-be8a-e61eedc2ff60": {
       "attrs": {
        "height": 0.8,
        "width": 0.8,
        "x": 0.1,
        "y": 0.1
       },
       "content": {
        "cell": "6103ff40-00f4-4b43-acec-d922586661f0",
        "part": "whole"
       },
       "id": "7b9f1541-92e5-43dd-be8a-e61eedc2ff60"
      }
     }
    },
    "ac1c491b-978f-46b2-a804-8b226ce12445": {
     "id": "ac1c491b-978f-46b2-a804-8b226ce12445",
     "prev": "08366e17-c8fb-4e81-ac4f-acb3ffdbc5db",
     "regions": {
      "996e4cd1-0a0f-45a3-97ae-c7128623f935": {
       "attrs": {
        "height": 0.8,
        "width": 0.8,
        "x": 0.1,
        "y": 0.1
       },
       "content": {
        "cell": "6810c05d-81ee-46bd-9d25-9defa201c74c",
        "part": "whole"
       },
       "id": "996e4cd1-0a0f-45a3-97ae-c7128623f935"
      }
     }
    },
    "c66f3737-7106-42fa-8299-56d309c7f58b": {
     "id": "c66f3737-7106-42fa-8299-56d309c7f58b",
     "prev": "07899ba6-25e4-4425-8a83-ced90a84d430",
     "regions": {
      "b378af1c-c140-404c-a0c1-bb405f6821ca": {
       "attrs": {
        "height": 0.8,
        "width": 0.8,
        "x": 0.1,
        "y": 0.1
       },
       "content": {
        "cell": "59231b4b-ee1d-47e6-b8d9-88ab33054553",
        "part": "whole"
       },
       "id": "b378af1c-c140-404c-a0c1-bb405f6821ca"
      }
     }
    },
    "cf589142-353f-4315-ace7-d5aa2c5cd3dd": {
     "id": "cf589142-353f-4315-ace7-d5aa2c5cd3dd",
     "prev": "a8f25ad5-2754-4843-aff0-773bbe089675",
     "regions": {
      "9fe1ee58-4cad-4808-a967-8ca4273a3fbf": {
       "attrs": {
        "height": 0.8,
        "width": 0.8,
        "x": 0.1,
        "y": 0.1
       },
       "content": {
        "cell": "3b98a228-fa37-4c93-9f1b-3357e2abf13d",
        "part": "whole"
       },
       "id": "9fe1ee58-4cad-4808-a967-8ca4273a3fbf"
      }
     }
    },
    "d2dac60f-d9f1-4455-9b2f-23f93393f241": {
     "id": "d2dac60f-d9f1-4455-9b2f-23f93393f241",
     "prev": "0c7aa757-342b-467c-8897-da18edc26325",
     "regions": {
      "8a6f6d21-f631-40a6-b464-fc8ec22ac744": {
       "attrs": {
        "height": 0.8,
        "width": 0.8,
        "x": 0.1,
        "y": 0.1
       },
       "content": {
        "cell": "9b5ecf88-9625-41e9-8e4d-b1c9ea1c1460",
        "part": "whole"
       },
       "id": "8a6f6d21-f631-40a6-b464-fc8ec22ac744"
      }
     }
    },
    "d6180808-ff71-4606-960f-ea33915d76fc": {
     "id": "d6180808-ff71-4606-960f-ea33915d76fc",
     "prev": "706271b3-dc1d-4004-8bda-8f6c8a9f71cb",
     "regions": {
      "89b4b565-9e0b-4d77-8cac-d2393bc6816d": {
       "attrs": {
        "height": 0.8,
        "width": 0.8,
        "x": 0.1,
        "y": 0.1
       },
       "content": {
        "cell": "5824b49d-7db6-4119-a8e0-fbb777008619",
        "part": "whole"
       },
       "id": "89b4b565-9e0b-4d77-8cac-d2393bc6816d"
      }
     }
    },
    "eebe3b35-f197-4cd1-885f-4536bd79fc1e": {
     "id": "eebe3b35-f197-4cd1-885f-4536bd79fc1e",
     "prev": "1c8fbe49-cf1a-4c13-9149-87396f5d2d43",
     "regions": {
      "7ac48262-cb7b-4944-8872-0fa33a910995": {
       "attrs": {
        "height": 0.8,
        "width": 0.8,
        "x": 0.1,
        "y": 0.1
       },
       "content": {
        "cell": "f3543e3c-c32e-4b02-818c-7567b72a098f",
        "part": "whole"
       },
       "id": "7ac48262-cb7b-4944-8872-0fa33a910995"
      }
     }
    }
   },
   "themes": {
    "default": "ca6d09d5-30f4-40f2-84be-550bb4cbd8c9",
    "theme": {
     "ca6d09d5-30f4-40f2-84be-550bb4cbd8c9": {
      "backgrounds": {
       "backgroundColor": {
        "background-color": "backgroundColor",
        "id": "backgroundColor"
       }
      },
      "id": "ca6d09d5-30f4-40f2-84be-550bb4cbd8c9",
      "palette": {
       "backgroundColor": {
        "id": "backgroundColor",
        "rgb": [
         34,
         34,
         34
        ]
       },
       "headingColor": {
        "id": "headingColor",
        "rgb": [
         256,
         256,
         256
        ]
       },
       "linkColor": {
        "id": "linkColor",
        "rgb": [
         66,
         175,
         250
        ]
       },
       "mainColor": {
        "id": "mainColor",
        "rgb": [
         256,
         256,
         256
        ]
       }
      },
      "rules": {
       "a": {
        "color": "linkColor"
       },
       "h1": {
        "color": "headingColor",
        "font-family": "Source Sans Pro",
        "font-size": 5.25
       },
       "h2": {
        "color": "headingColor",
        "font-family": "Source Sans Pro",
        "font-size": 4
       },
       "h3": {
        "color": "headingColor",
        "font-family": "Source Sans Pro",
        "font-size": 3.5
       },
       "h4": {
        "color": "headingColor",
        "font-family": "Source Sans Pro",
        "font-size": 3
       },
       "h5": {
        "color": "headingColor",
        "font-family": "Source Sans Pro"
       },
       "h6": {
        "color": "headingColor",
        "font-family": "Source Sans Pro"
       },
       "h7": {
        "color": "headingColor",
        "font-family": "Source Sans Pro"
       },
       "li": {
        "color": "mainColor",
        "font-family": "Source Sans Pro",
        "font-size": 6
       },
       "p": {
        "color": "mainColor",
        "font-family": "Source Sans Pro",
        "font-size": 6
       }
      },
      "text-base": {
       "color": "mainColor",
       "font-family": "Source Sans Pro",
       "font-size": 6
      }
     },
     "faf24a67-1574-42ab-ab1c-3c53b95bbe05": {
      "backgrounds": {
       "backgroundColor": {
        "background-color": "backgroundColor",
        "id": "backgroundColor"
       }
      },
      "id": "faf24a67-1574-42ab-ab1c-3c53b95bbe05",
      "palette": {
       "backgroundColor": {
        "id": "backgroundColor",
        "rgb": [
         34,
         34,
         34
        ]
       },
       "headingColor": {
        "id": "headingColor",
        "rgb": [
         238,
         238,
         238
        ]
       },
       "linkColor": {
        "id": "linkColor",
        "rgb": [
         170,
         34,
         51
        ]
       },
       "mainColor": {
        "id": "mainColor",
        "rgb": [
         238,
         238,
         238
        ]
       }
      },
      "rules": {
       "a": {
        "color": "linkColor"
       },
       "h1": {
        "color": "headingColor",
        "font-family": "Ubuntu",
        "font-size": 7
       },
       "h2": {
        "color": "headingColor",
        "font-family": "Ubuntu",
        "font-size": 5
       },
       "h3": {
        "color": "headingColor",
        "font-family": "Ubuntu",
        "font-size": 3.75
       },
       "h4": {
        "color": "headingColor",
        "font-family": "Ubuntu",
        "font-size": 3
       },
       "h5": {
        "color": "headingColor",
        "font-family": "Ubuntu"
       },
       "h6": {
        "color": "headingColor",
        "font-family": "Ubuntu"
       },
       "h7": {
        "color": "headingColor",
        "font-family": "Ubuntu"
       },
       "li": {
        "color": "mainColor",
        "font-family": "Ubuntu",
        "font-size": 5
       },
       "p": {
        "color": "mainColor",
        "font-family": "Ubuntu",
        "font-size": 5
       }
      },
      "text-base": {
       "color": "mainColor",
       "font-family": "Ubuntu",
       "font-size": 5
      }
     }
    }
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
